package com.cxh.dataservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cxh.dataservice.config.DataSourceStrategyFactory;
import com.cxh.dataservice.entity.EpOperatorInterpreter;
import com.cxh.dataservice.entity.SysUserTest;
import com.cxh.dataservice.service.DataSourceService;
import com.cxh.dataservice.service.IEpOperatorInterpreterService;
import com.cxh.dataservice.service.ScriptService;
import com.cxh.dataservice.service.TestService;
import com.cxh.dataservice.service.impl.TestServiceImpl;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

/**
 * Description: <br/>
 * date: 2020/6/18 13:59<br/>
 *
 * @author ChenXh<br />
 */
@RestController
public class GroovyController {
    @Autowired
    private Binding groovyBinding;
    @Autowired
    private IEpOperatorInterpreterService epOperatorInterpreterService;

    private GroovyShell groovyShell;

    @PostConstruct
    public void init(){
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader(this.getClass().getClassLoader());
        CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
        compilerConfiguration.setSourceEncoding("utf-8");
        //compilerConfiguration.setScriptBaseClass(TestScript.class.getName());

        groovyShell = new GroovyShell(groovyClassLoader, groovyBinding, compilerConfiguration);
    }

    @PostMapping("/runGroovy")
    public String runGroovy(@RequestBody String groovyStr){
        JSONObject jsonObject = JSONObject.parseObject(groovyStr);
        jsonObject.get("param").toString();
        Script script = groovyShell.parse(jsonObject.get("groovyStr").toString());
        return String.valueOf(script.run());
    }

    @PostMapping("/run")
    public String run(@RequestBody Map<String,String> param){
        EpOperatorInterpreter epOperatorInterpreter = epOperatorInterpreterService.getOne(new LambdaQueryWrapper<EpOperatorInterpreter>()
                .eq(EpOperatorInterpreter::getOperatorInterCode, param.get("key")));

        if("SQL".equals(epOperatorInterpreter.getInterpreterType())){
            DataSourceService dataSourceService = (DataSourceService)DataSourceStrategyFactory.getData(epOperatorInterpreter.getInterpreterDatasource());
            return JSONObject.toJSON(dataSourceService.getData(epOperatorInterpreter.getInterpreterContent())).toString();
        }else{
            groovyBinding.setProperty("param",param);
            Script script = groovyShell.parse(epOperatorInterpreter.getInterpreterContent());
            return String.valueOf(script.run());
        }
    }

}
